package 学生管理系统;

import java.util.ArrayList;
import java.util.Scanner;



public class Student_test {
    private static final String ADD_SSTUDENT="1";
    private static final String DELETE_SSTUDENT="2";
    private static final String UPDATE_SSTUDENT="3";
    private static final String QUERY_SSTUDENT="4";
    private static final String EXIT="5";

    public static void startStudentsystem() {
        ArrayList<Student> list=new ArrayList<>();
        Scanner sc = new Scanner(System.in);
        loop:
        while (true) {
            System.out.println("-------------------欢迎来到学生管理系统------------------");
            System.out.println("1.添加学生");
            System.out.println("2.删除学生");
            System.out.println("3.修改学生");
            System.out.println("4.查找学生");
            System.out.println("5.退出");
            System.out.println("输入你的选择：");
            String select = sc.next();
            switch (select) {
                case ADD_SSTUDENT -> add(list);
                case DELETE_SSTUDENT -> delete(list);
                case UPDATE_SSTUDENT -> update(list);
                case QUERY_SSTUDENT -> serch(list);
                case EXIT -> {
                    System.out.println("退出");
                    //break loop;
                    // 停止虚拟机运行
                    System.exit(0);
                }

                default -> System.out.println("没有这个选项");
            }
        }
    }
    public static void add(ArrayList<Student> list){
        Scanner sc=new Scanner(System.in);
        System.out.println("请输入学生学号");
        String id= sc.next();

        System.out.println("请输入学生姓名");
        String name=sc.next();

        System.out.println("请输入学生年龄");
        int age=sc.nextInt();

        System.out.println("请输入学生家庭住址");
        String address=sc.next();

        Student stu=new Student(id,name,age,address);

        list.add(stu);
    }
    public static void delete(ArrayList<Student> list){
        Scanner sc=new Scanner(System.in);
        System.out.println("请输入要删除的学生id");
        String id = sc.next();
        // 判断id是否存在
        if(idContains(list,id)){
            // id存在
            // 获取id所在的索引 并删除
            int index=getIndex(list,id);
            list.remove(index);
            System.out.println("删除成功");
        }else{
            // id 不存在
            System.out.println("id不存在");
            return;
        }
    }
    public static void update(ArrayList<Student> list){
        Scanner sc=new Scanner(System.in);
        System.out.println("请输入要删除的学生id");
        String id = sc.next();
        // 判断id是否存在
        if(idContains(list,id)){
            // id存在
            // 获取id所在的索引 并修改信息
            int index=getIndex(list,id);
            Student stu=list.get(index);
            while (true) {
                System.out.println("请输入学生学号");
                id = sc.next();
                // 判断id是否存在
                if(idContains(list,id)){
                    System.out.println("学号存在 请重新输入学生学号：");
                }else{
                    break;
                }
            }

            System.out.println("请输入学生姓名");
            String name=sc.next();

            System.out.println("请输入学生年龄");
            int age=sc.nextInt();

            System.out.println("请输入学生家庭住址");
            String address=sc.next();
            stu.setAge(age);
            stu.setId(id);
            stu.setName(name);
            stu.setAddress(address);
            System.out.println("修改成功");
        }else{
            // id 不存在
            System.out.println("id不存在");
            return;
        }
    }
    public static void serch(ArrayList<Student> list){
        if(list.size()==0){
            System.out.println("学生信息为空");

        }else{
            System.out.println("学号\t姓名\t年龄\t家庭住址\t");
            for (int i = 0; i < list.size(); i++) {
                System.out.println(list.get(i).getId()+"\t"+list.get(i).getName()+"\t"+list.get(i).getAge()+"\t"+list.get(i).getAddress());
            }
        }
    }

    public static boolean idContains(ArrayList<Student> list,String id){
        //for (int i = 0; i < list.size(); i++) {
        //    if (list.get(i).getId().equals(id)) {
        //        return true;
        //    }
        //}
        //return false;
        return getIndex(list,id)>=0;
    }
    public static int getIndex(ArrayList<Student> list,String id){
        for (int i = 0; i < list.size(); i++) {
            if(list.get(i).getId().equals(id)){
                return i;
            }
        }
        return -1;
    }
}